/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.Visit;
import com.sxf.crm.enums.VisitStatus;
import com.sxf.crm.enums.VisitType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 拜访数据访问接口
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/6/1
 */
@Repository
public interface VisitRepository extends JpaRepository<Visit, Long> {
    List<Visit> findByUserId(Long userId);
    List<Visit> findByCustomerId(Long customerId);
    
    @Query("SELECT v FROM Visit v WHERE " +
           "(:title IS NULL OR v.title LIKE %:title%) AND " +
           "(:visitType IS NULL OR v.visitType = :visitType) AND " +
           "(:visitStatus IS NULL OR v.visitStatus = :visitStatus) AND " +
           "(:userId IS NULL OR v.user.id = :userId) AND " +
           "(:customerId IS NULL OR v.customer.id = :customerId)")
    Page<Visit> findByConditions(
            @Param("title") String title,
            @Param("visitType") VisitType visitType,
            @Param("visitStatus") VisitStatus visitStatus,
            @Param("userId") Long userId,
            @Param("customerId") Long customerId,
            Pageable pageable);
    
    // 统计方法
    long countByVisitTimeBetween(Long startTime, Long endTime);
} 